 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upc.clase.demo.controller;

import edu.upc.clase.demo.entity.Categoria;
import edu.upc.clase.demo.entity.Compras;
import edu.upc.clase.demo.entity.Credential;
import edu.upc.clase.demo.entity.CriterioBusqueda;
import edu.upc.clase.demo.entity.PlanNegocio;
import edu.upc.clase.demo.entity.Usuario;
import edu.upc.clase.demo.service.CategoriaService;
import edu.upc.clase.demo.service.ComprasService;
import edu.upc.clase.demo.service.PlanNegocioService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.omg.CORBA.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;


/**
 *
 * @author PcWiene
 */

@Controller("planNegocioController")
public class PlanNegocioController {

       
    private static Logger log = LoggerFactory.getLogger(UsuarioController.class);
    
    @Autowired
    private PlanNegocioService planNegocioServicio;

    @Autowired
    private ComprasService compraServicio;;
    
    @RequestMapping("/planes/buscar_plan")
    public ModelAndView buscarPlan() {
        
        ModelAndView mav = new ModelAndView("planes/buscar_plan");
        PlanNegocio p = new PlanNegocio();
        mav.getModelMap().put("planes", p);
        return mav;
    }
    
    @RequestMapping(value="/planes/comprar_plan", method = RequestMethod.GET)
    public ModelAndView comprarPlan(HttpServletRequest request){
        ModelAndView mav = new ModelAndView("planes/comprar_plan");
        List<PlanNegocio> P = planNegocioServicio.buscarTodos();
        Compras c = new Compras();
        c.setPlan_negocio_id_plan(1);

        Credential cr = (Credential)request.getSession().getAttribute("credential");
        c.setUsuario_id_usuario(cr.getId());
        
        mav.addObject("compra", c);
        mav.addObject("plan", P);
        
        return mav;
    }
    
    @RequestMapping(value = "/planes/comprar_plan", method = RequestMethod.POST)
    public String comprarPlan(@ModelAttribute("compra")Compras c, SessionStatus status) {
        System.out.println("d: " + c.getDireccion());
        System.out.println("f: " + c.getFe_caducidad());
        System.out.println("c: " + c.getNu_tarjeta());
        compraServicio.insertar(c);
        status.setComplete();
        return "redirect:/pages/planes/comprar_plan ";
    }
    
    
    /*
        
    @RequestMapping(value = "/planes/mante_emprendedores", method = RequestMethod.GET)
    public ModelAndView manteEmprendedores() {
        ModelAndView mav = new ModelAndView("planes/mante_emprendedores");
        Usuario usuario = new Usuario();
        usuario.setTipo_usuario("E");
        usuario.setIn_estado("1");
        mav.getModelMap().put("usuario", usuario);
        return mav;
    }

    */

           
    @RequestMapping(value = "/planes/mante_plan", method = RequestMethod.GET)
    public ModelAndView mantePlan(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("planes/mante_plan");
        List<PlanNegocio> P = planNegocioServicio.buscarTodos();
        PlanNegocio e = new PlanNegocio();
        e.setCategoria_id_categoria(1);
        Credential cr = (Credential)request.getSession().getAttribute("credential");
        e.setUsuario_id_usuario(cr.getId());
        
        e.setEstado_plan("1");
        e.setIn_estado("1");
        mav.addObject("plan", P);
        mav.addObject("e", e);
        return mav;
    }

    @RequestMapping(value = "/planes/mante_plan", method = RequestMethod.POST)
    public String mantePlan(@ModelAttribute("plan")PlanNegocio p, SessionStatus status) {
        System.out.println("p " + p.getTitulo());
        planNegocioServicio.insertar(p);
        status.setComplete();
        return "redirect:/pages/planes/mante_plan";
    }

    /*
    @RequestMapping(value = "/planes/mante_sabio", method = RequestMethod.GET)
    public ModelAndView manteSabio() {
        ModelAndView mav = new ModelAndView("planes/mante_sabio");
        Usuario usuario = new Usuario();
        usuario.setTipo_usuario("E");
        usuario.setIn_estado("1");
        mav.getModelMap().put("usuario", usuario);
        return mav;
    } */
    

    
    
    @RequestMapping("/planes/principal")
    public ModelAndView principal() {
        
        ModelAndView mav = new ModelAndView("planes/principal");
        PlanNegocio p = new PlanNegocio();
        mav.getModelMap().put("planes", p);
        return mav;
    }

    
    @RequestMapping(value = "/planes/regist_planes", method = RequestMethod.GET)
    public ModelAndView newPlanes(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("planes/regist_planes");
        PlanNegocio p = new PlanNegocio();
        p.setCategoria_id_categoria(1);
        Credential cr = (Credential)request.getSession().getAttribute("credential");
        p.setUsuario_id_usuario(cr.getId());
        
        p.setEstado_plan("1");
        p.setIn_estado("1");
        mav.getModelMap().put("plan", p);
        return mav;
    }   
    
    @RequestMapping(value = "/planes/regist_planes", method = RequestMethod.POST)
    public String createPlanes(@ModelAttribute("plan")PlanNegocio p, SessionStatus status) {
        System.out.println("p " + p.getTitulo());
        planNegocioServicio.insertar(p);
        status.setComplete();
        return "redirect:/pages/planes/regist_planes";
    }
    
    
    @RequestMapping("/planes/subir_plan")
    public ModelAndView subirPlan() {
        
        ModelAndView mav = new ModelAndView("planes/subir_plan");
        PlanNegocio p = new PlanNegocio();
        mav.getModelMap().put("planes", p);
        
        return mav;
    }    
        
    @RequestMapping(value = "/planes/validar_plan", method = RequestMethod.GET)
    public ModelAndView validarPlan() {
        ModelAndView mav = new ModelAndView("planes/validar_plan");
        Usuario usuario = new Usuario();
        usuario.setTipo_usuario("E");
        usuario.setIn_estado("1");
        mav.getModelMap().put("usuario", usuario);
        return mav;
    } 
    

    
    
    
    @RequestMapping("/planes/monitorear_planes")
    public ModelAndView monitorearPlanes() {
        ModelAndView mav = new ModelAndView("planes/monitorear_planes");
        List<PlanNegocio> P = planNegocioServicio.buscarTodos();
        
        mav.addObject("plan", P);
        
        return mav;
    }    



}
